package com.bd.scm.config;

import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * 描述：ModelMapper 配置
 */
@Configuration
public class ModelMapperConfig {

    @Bean
    @Scope("singleton")
    public ModelMapper getModelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        //默认为standard模式，设置为strict模式
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

        // 类型映射代码
//        sourceSonToDestinationSon(modelMapper);
//        sourceToDestination(modelMapper);

        //验证映射
        modelMapper.validate();

        // 配置代码
        return modelMapper;
    }

//    private void sourceSonToDestinationSon(ModelMapper modelMapper) {
//        modelMapper.createTypeMap(SouSubClass.class, DestSubClass.class)
//                .addMapping(SouSubClass::getSonId, DestSubClass::setDsonId)
//                .addMapping(SouSubClass::getSonName, DestSubClass::setSonName)
//                .addMappings(mapper -> mapper.skip(DestSubClass::setExcessParam));
//    }
//
//    private void sourceToDestination(ModelMapper modelMapper) {
//        modelMapper.createTypeMap(Source.class, Destination.class)
//                .addMappings(mapper -> mapper.using((Converter<Integer, Integer>) context -> {
//                        if (context.getSource() == null) {
//                            return 18;
//                        }
//                        return context.getSource();
//                    }).map(Source::getAge, Destination::setAge))
//                .addMappings(mapper -> mapper.using((Converter<Date, Date>) context -> {
//                        if (context.getSource() == null) {
//                            return new Date();
//                        }
//                        return context.getSource();
//                    }).map(Source::getCreateTime, Destination::setCreateTime))
//                .addMapping(Source::getSourceSon, Destination::setDestinationSon)
//                .addMapping(Source::getListSon, Destination::setSonList)
//                .addMapping(Source::getMapSon, Destination::setSonMap)
//                .addMappings(mapper -> mapper.skip(Destination::setExcessParam));
//    }
}
